package com.ruoyi.demo.myservice;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.exception.user.UserException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@RequiredArgsConstructor
@Service
@Slf4j
public class MyLoginService {
    /**
     * 检测一下J平台的token 对不对
     *
     * @param token
     * @return
     */
    public String checkJToken(String token) throws IOException {
        String url = "http://localhost:9998/oauth/fjw";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("token",token);
        CloseableHttpResponse response = null;
        response = httpClient.execute(httpPost);
        // 4.获取返回值
        HttpEntity entity = response.getEntity();
        String body = EntityUtils.toString(entity, "utf-8");
        JSONObject json = JSONObject.parseObject(body);
        R r = JSON.toJavaObject(json, R.class);
        if(r.getCode()!=200){
            response.close();
            log.info("第三方token：{} 异常.", token);
            throw new UserException("user.not.exists", token);
        }else {
            response.close();
            return r.getData().toString();
        }
    }

    /**
     * 向第三方库获取token
     * @param code
     * @return
     * @throws IOException
     */
    public String  getToken(String code) throws IOException {
        String url = "http://localhost:9998/oauth/token";
        //创建httpClient对象
        CloseableHttpClient client = HttpClients.createDefault();
        //创建post方式请求对象
        HttpPost httpPost = new HttpPost(url);
        List nvps = new ArrayList<>();

        nvps.add(new BasicNameValuePair("client_id", "yLBv6UPJc1"));
        nvps.add(new BasicNameValuePair("client_secret", "4HA6fMozXn"));
        nvps.add(new BasicNameValuePair("grant_type", "authorization_code"));
        nvps.add(new BasicNameValuePair("code", code));
        nvps.add(new BasicNameValuePair("redirect_uri", "http://localhost/login"));
        CloseableHttpResponse response = null;
        httpPost.setEntity(new UrlEncodedFormEntity(nvps,"utf-8"));
        httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
        response = client.execute(httpPost);
        if(response.getStatusLine().getStatusCode() !=200){
            response.close();
            log.info("授权码：{} 异常.", code);
            throw new UserException("user.not.exists", code);
        }
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            String body = EntityUtils.toString(entity, "utf-8");
            JSONObject json = JSONObject.parseObject(body);
            String token = json.getString("access_token");
            response.close();
            return token;
        }else {
            response.close();
            log.info("第三方异常没有获得token" );
            throw new UserException("user.not.exists",code);
        }
    }
}
